import * as Cesium from "cesium";
import "cesium/Build/Cesium/Widgets/widgets.css";
import Log from "../../plugins/utils/log";
import Environment from "../../plugins/utils/environment";

window.CESIUM_BASE_URL = "./Cesium/V1.70.1/";
Cesium.Ion.defaultAccessToken =
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI3MDlmNjQ1Yy0yOTEzLTQwOGUtODg4OC1mNmNlN2M2NDBiNDgiLCJpZCI6MTg1NTksInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1NzQwNTc1Mjd9.JSreNG--SBKAAUCdAFnxhRBK_4CqOAsgMODgpJsbz1M";


export default class Map{
    static init(){
        let imageProvider = null;
        let terrainProvider = null;
      //  Setting.DataSource = Setting.DataSourceType.HTTP_DATA

       // Setting.DataSource = Setting.DataSourceType.BASE_DATA
        if (Setting.DataSource == Setting.DataSourceType.HTTP_DATA) {
            // imageProvider = new Cesium.UrlTemplateImageryProvider({
            //     url: Setting.HTTP_IMAGE_LAYER_URL
            // });
            var subdomains=['0','1','2','3','4','5','6','7'];
            imageProvider=new Cesium.WebMapTileServiceImageryProvider({
                url: "http://t{s}.tianditu.com/img_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=28890177b959659a402f18fe3af41dc1",
                layer: "tdtBasicLayer",
                subdomains: subdomains,
                style: "default",
                format: "image/jpeg",
                tileMatrixSetID: "GoogleMapsCompatible"
            });
            if (Setting.terrainEnabled == true) {
                if (Setting.HTTP_TERRAIN_LAYER_URL.length > 0) {
                    terrainProvider = new Cesium.CesiumTerrainProvider({
                        url: Setting.HTTP_TERRAIN_LAYER_URL
                    });
                } else {
                    terrainProvider = Cesium.createWorldTerrain();
                }
            }

             terrainProvider = Cesium.createWorldTerrain();
             
              //viewer.terrainProvider = terrainProvider;


        } else if(Setting.DataSource == Setting.DataSourceType.ANDROID_DATA){
            imageProvider = new Cesium.UrlTemplateImageryProvider({
                //url: "http://127.0.0.1:9999/BaseImage/" + "{z}/{x}/{y}"
                url: "http://mev.com/baseimage?datatype=baseimagepak&x={x}&y={y}&z={z}",
                maximumLevel : 10
            });
            terrainProvider = new Cesium.CesiumTerrainProvider({
                //url: "http://127.0.0.1:9999/BaseDem/"
                url: "http://mev.com/baseimage?datatype=basedempak&x={x}&y={y}&z={z}"
            });
        }
        else if(Setting.DataSource == Setting.DataSourceType.BASE_DATA)
        {
            imageProvider = new Cesium.TileMapServiceImageryProvider({
                url: Cesium.buildModuleUrl("Assets/Textures/NaturalEarthII"),
              });
              terrainProvider = null;

        }

        let viewer = new Cesium.Viewer("cesiumContainer", {
            animation: false,
            baseLayerPicker: false,
            fullscreenButton: false,
            geocoder: false,
            timeline: false,
            shouldAnimate: false,
            sceneModePicker: false,
            navigationHelpButton: false,
            infoBox: false,
            selectionIndicator: false,
            homeButton: false,
            skyBox: false,
            contextOptions: {
                webgl:{
                    alpha: true,
                    depth:true,
                    stencil:true,
                    antialias:true,
                    premultipliedAlpha:true,
                    //通过canvas.toDataURL()实现截图需要将该项设置为true
                    preserveDrawingBuffer:true,
                    failIfMajorPerformanceCaveat:true
                }
            },
            imageryProvider: imageProvider,
            terrainProvider: terrainProvider
        });
        viewer.scene.debugShowFramesPerSecond = false;
        // viewer.scene.debugShowFramesPerSecond = false;

        //开启深度测试
        // viewer.scene.globe.depthTestAgainstTerrain = false;
        //自动匹配字体分辨率
        viewer.scene.fxaa = true;
        viewer.scene.postProcessStages.fxaa.enabled = true;

        if (Cesium.FeatureDetection.supportsImageRenderingPixelated()) {
            viewer.resolutionScale = window.devicePixelRatio;
        }

        viewer.cesiumWidget.creditContainer.style.display = "none";
        //viewer.animation.container.style.display = "none";
       // viewer.timeline.container.style.display = "none";

        window.cesiumViewer = viewer;
        Map.initFuns();
        return viewer;
    }

    static initFuns(){
        window.cesiumPickPosition = function(position) {
            let viewer = window.cesiumViewer;
            let ray = viewer.camera.getPickRay(position);
            if (ray) {
                let cartesian3 = viewer.scene.globe.pick(ray, viewer.scene);
                return cartesian3;
            }
            return null;
        };
    }
}
